/**
 *  hw_irq.c - Hardware interfaces for interruption.
 *
 *  Copyright (C) 2008  ZhangHu
 *  All rights reserved.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  E-MAIL: anmnmnly@gmail.com
 */


#include "regdef.h"
#include "hw_irq.h"
#include "include/irq.h"
#include "include/core.h"

/**
 * init_tick - select a timer for OS tick
 * @return:
 *
 * @notes: Initialize timer, clock cycle is 10ms.
 */
void init_tick(void) {
    isr_install(SYS_TIMER, 0, systick);
    regTimer1Load = 0xFF;
    regTimer1Control = (TIMER_ENABLE | TIMER_PERIODIC | TIMER_PRESCALE8 );
    regIRQEnableSet = IRQ_TIMER1;
}

/**
 * get_isr_num - Get the interrupt number, when interrupt occured.
 * @return: The serial number of active interruption.
 *
 * @notes: The serial number depends on specifical CPU.
 */
uword_t get_isr_num(void) {
    uword_t status = regIRQStatus;

    if(status & IRQ_TIMER1) {
        return INT_TIMER1;
    } else if(status & IRQ_TIMER2) {
        return INT_TIMER2;
    }
    return INT_TIMER2 + 1;
}

/**
 * isr_after_handle - clean something when interruption had handled.
 * @return:
 *
 * @notes: we shoulde rewrite this routine for Different CPU.
 */
void isr_after_handle(uword_t isr_num) {
    if(isr_num == INT_TIMER1) {
        regTimer1Clear = 0;
    } else if(isr_num == INT_TIMER2) {
        regTimer2Clear = 0;
    }
}

